<?php
class sis_setup_parameter_model extends MY_Model {
	var $model = 'sis_setup_parameter';
	
	function GetJSON($param=array())
	{
		$result		= $this -> db->query("SELECT ID_VARIABEL, NAMA_VARIABEL FROM SIS_SETUP_VARIABEL");
		$arr_data	= $result->result_array();
		
		$ls_sql	= "	SELECT	TO_CHAR(TGL_EFEKTIF,'DD/MM/YYYY') TGL_EFEKTIF, '".implode("|",$param)."' PARAM";
			for($i=0, $max_i=count($arr_data); $i<$max_i; $i++){
				$row = $arr_data[$i];
				
				$arr_jnsagt = $this->GetAllJenisAnggota();
				for($j=0, $max_j=count($arr_jnsagt); $j<$max_j; $j++){
					$row_jnsagt	= $arr_jnsagt[$j];
					$kd_jenis_anggota	= $row_jnsagt['kd_jenis_anggota'];
					
					$ls_sql	.= ", (SELECT X.NILAI FROM SIS_SETUP_PARAMETER X WHERE X.ID_VARIABEL='".$row['id_variabel']."' AND KD_JENIS_ANGGOTA = '".$kd_jenis_anggota."' AND X.TGL_EFEKTIF = TBL.TGL_EFEKTIF) " . str_replace('.','_',$row['id_variabel']) . "_".$kd_jenis_anggota." ";
					//$ls_sql	.= ", NVL(F_SIS_GET_VARIABEL_PARAMETER('".$row['id_variabel']."', '".$kd_jenis_anggota."', TBL.TGL_EFEKTIF),0) " . $row['id_variabel'] . "_".$kd_jenis_anggota." ";
					//$ls_sql	.= ", (SELECT X.NILAI FROM SIS_SETUP_PARAMETER X WHERE X.ID_VARIABEL='".$row['id_variabel']."' AND X.TGL_EFEKTIF = TBL.TGL_EFEKTIF) " . $row['id_variabel'] . "_".$kd_jenis_anggota." ";
				}
				
			}
		$ls_sql	.= "FROM    ( SELECT DISTINCT TGL_EFEKTIF FROM SIS_SETUP_PARAMETER WHERE ID_VARIABEL IN ('".implode("','",$param)."')) TBL
					WHERE	1 = 1
					ORDER BY ".(post('sort')=='' ? 'TBL.TGL_EFEKTIF' : post('sort'))." ".(post('order')=='' ? 'DESC' : post('order'));
		
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('SIS_SETUP_PARAMETER', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('SIS_SETUP_PARAMETER', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_idvariabel, $kd_jenis_anggota, $eff_date){
		$result = $this->db->get_where('SIS_SETUP_PARAMETER', array('ID_VARIABEL' => $as_idvariabel, 'KD_JENIS_ANGGOTA' => $kd_jenis_anggota, 'TGL_EFEKTIF' => $eff_date));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_idvariabel, $kd_jenis_anggota, $eff_date)
	{
		$result = $this->db->get_where('SIS_SETUP_PARAMETER', array('ID_VARIABEL' => $as_idvariabel, 'KD_JENIS_ANGGOTA' => $kd_jenis_anggota, 'TGL_EFEKTIF' => $eff_date));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('SIS_SETUP_PARAMETER', $arr_rule);
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
	
	function GetAllJenisAnggota(){
		$result	= $this->db->query("SELECT kd_jenis_anggota, nama_jenis_anggota FROM sis_setup_jenis_anggota ORDER BY kd_jenis_anggota");
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}
}
?>